#include "dk200client.h"

Dk200Client::Dk200Client(): IceBase()
{
    cout<<"Dk200Client create"<<endl;
}

int Dk200Client::request(int type,  QStringList &info)
{
    cout<<"in Dk200Client::request"<<endl;
#ifdef ICE_STATIC_LIBS
    Ice::registerIceSSL();
    Ice::registerIceUDP();
    Ice::registerIceWS();
#endif

    try
    {
        QString configFile;
        configFile = QString("config200dk.client");

        Ice::CommunicatorHolder ich(configFile.toStdString());
        auto communicator = ich.communicator();

        auto  objIdentify =Ice::uncheckedCast<ObjIdentifyPrx>(communicator->propertyToProxy("Classify.Proxy"));
        if (type == 1)
        {
            kModelWidth = 416;
            kModelHeight = 416;
            ModelPath = "../yolov3_ice/model/yolov3_hands.om";
            objIdentify =Ice::uncheckedCast<ObjIdentifyPrx>(communicator->propertyToProxy("Detect.Proxy"));
        }
         else if (type == 2)
        {
            kModelWidth = 224;
            kModelHeight = 224;
            ModelPath = "../acl_classification/model/mobilenetv2_new.om";

        }else if(type == 3){

        }
        else
        {
            cout<<"parameter error"<<endl;
        }


        if (!objIdentify)
        {
            throw "Invalid proxy";
        }
        ::IdentifyModule::AlgorithmInfo algo_info = objIdentify->GetAlgorithmInfo();
        cout << "Contents of GetAlgorithmInfo:" << endl;
        cout << "name=" << algo_info.name << endl;
        cout << "version=" << algo_info.version << endl;
        info<< QString::fromStdString( algo_info.name) <<QString::fromStdString( algo_info.version);

    }
    catch(const std::exception& ex)
    {
        cerr << ex.what() << endl;
        return -1;
    }
    return 0;
}

qint64 Dk200Client::getRequestTimeSpan()
{
    return requestTimeSpan;
}



int Dk200Client::request(int type, Mat frame ,::IdentifyModule::BboxSeq &objKind)
{


#ifdef ICE_STATIC_LIBS
    Ice::registerIceSSL();
    Ice::registerIceUDP();
    Ice::registerIceWS();
#endif

    try
    {
        QString configFile;
        configFile = QString("config200dk.client");
        Ice::CommunicatorHolder ich(configFile.toStdString());
        auto communicator = ich.communicator();
        auto objIdentify =Ice::uncheckedCast<ObjIdentifyPrx>(communicator->propertyToProxy("Classify.Proxy"));
        if (type == 1)
        {

             kModelWidth = 416;
             kModelHeight = 416;
             ModelPath = "../yolov3_ice/model/yolov3_hands.om";
        }
         else if (type == 2)
        {
            kModelWidth = 224;
            kModelHeight = 224;
            ModelPath = "../acl_classification/model/mobilenetv2_new.om";
        }else if(type == 3){

        }
        else
        {
            cout<<"parameter error"<<endl;
            return -1;
        }




        if (!objIdentify)
        {
            throw "Invalid proxy";
        }

        objIdentify->SetDevice(deviceID);
        objIdentify->SetModelPath(ModelPath, WeightPath);

        ::IdentifyModule::Row Img;
        imgToRow(frame, Img);
        requestTimeSpan = QDateTime::currentDateTime().toMSecsSinceEpoch();
        objKind = objIdentify->GetObjBbox(Img);
        requestTimeSpan = QDateTime::currentDateTime().toMSecsSinceEpoch()- requestTimeSpan;
        for (int i = 0; i < objKind.size(); i++)
        {
            cout << "name=" << objKind[i].name << endl;
            cout << "confidence=" << objKind[i].confidence << endl;
            cout << "position= ( " << objKind[i].rect.left << "," << objKind[i].rect.top << ",";
            cout << objKind[i].rect.right << "," << objKind[i].rect.bottom << ")" << endl;            
        }


    }
    catch(const std::exception& ex)
    {
        cerr << ex.what() << endl;
        return -1;
    }
    return 0;


}


int Dk200Client::request(int type, string file ,::IdentifyModule::BboxSeq &objKind)
{
#ifdef ICE_STATIC_LIBS
    Ice::registerIceSSL();
    Ice::registerIceUDP();
    Ice::registerIceWS();
#endif

    try
    {
        QString configFile;
        configFile = QString("config200dk.client");

        cout<< configFile.toStdString() <<endl;
        Ice::CommunicatorHolder ich(configFile.toStdString());
        auto communicator = ich.communicator();

        auto  objIdentify =Ice::uncheckedCast<ObjIdentifyPrx>(communicator->propertyToProxy("Classify.Proxy"));
        if (type == 1)
        {

             kModelWidth = 416;
             kModelHeight = 416;
             ModelPath = "../yolov3_ice/model/yolov3_hands.om";
             objIdentify =Ice::uncheckedCast<ObjIdentifyPrx>(communicator->propertyToProxy("Detect.Proxy"));
        }
         else if (type == 2)
        {
            kModelWidth = 224;
            kModelHeight = 224;
            ModelPath = "../acl_classification/model/mobilenetv2_new.om";

        }else if(type == 3){

        }
        else
        {
            cout<<"parameter error"<<endl;
        }

        if (!objIdentify)
        {
            throw "Invalid proxy";
        }

        objIdentify->SetDevice(deviceID);
        objIdentify->SetModelPath(ModelPath, WeightPath);

        ::IdentifyModule::Row Img;
        imgToRow(file, Img);

        objKind = objIdentify->GetObjBbox(Img);
        cout << "Contents of getObjKinds:" << endl;
        for (int i = 0; i < objKind.size(); i++)
        {
            cout << "name=" << objKind[i].name << endl;
            cout << "confidence=" << objKind[i].confidence << endl;
            cout << "position= ( " << objKind[i].rect.left << "," << objKind[i].rect.top << ",";
            cout << objKind[i].rect.right << "," << objKind[i].rect.bottom << ")" << endl;
        }

    }
    catch(const std::exception& ex)
    {
        cerr << ex.what() << endl;
        return 0;
    }
    return 0;

}


